//
//  CategoryTableViewController.m
//  EasyTip
//
//  Created by Jeffrey Palm on 2/14/09.
//  Copyright 2009 Jeffrey Palm. All rights reserved.
//

#import "EasyTipAppDelegate.h"
#import "CategoryTableViewController.h"
#import "Category.h"
#import "Criteria.h"
#import "SummaryViewController.h"
#import "CriteriaTableViewCell.h"
#import "Util.h"


@implementation CategoryTableViewController

@synthesize category;

- (id) init:(Category*)cat {
	if (self = [super init]) {
		self.category = cat;
		self.title = category.name;
		self.tableView.delegate = self;
		self.tableView.dataSource = self;
	}
	return self;
}


/*
- (id)initWithStyle:(UITableViewStyle)style {
    // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
    if (self = [super initWithStyle:style]) {
    }
    return self;
}
*/

/*
- (void)viewDidLoad {
    [super viewDidLoad];

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
*/

/*
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
*/
/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
	[super viewDidDisappear:animated];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}

#pragma mark Table view methods

// decide what kind of accesory view (to the far right) we will use
- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath
{
	return UITableViewCellAccessoryDisclosureIndicator;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	NSInteger i = (NSInteger)[NSNumber numberWithInt:[category criteriaCount]];
	return [i intValue];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	    
    static NSString *CellIdentifier = @"Cell";
    
    CriteriaTableViewCell *cell = (CriteriaTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[CriteriaTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }
	
  // Set up the cell...
	NSLog(@"[CategoryTableViewController] have index row %i", indexPath.row);
	Criteria *c = (Criteria*)[category criteriaAt:indexPath.row];
	if (c.imageName) {
		//cell.image = [Util scaleAndRotateImage:[UIImage imageNamed:c.imageName] maxResolution:42];
		cell.image = [UIImage imageNamed:c.imageName];
	}
	cell.title  = c.name;
	cell.description = c.description ? c.description : @"TODO";	
	
  return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

	// Accumulate the value for this category
	int index = indexPath.row;
	Criteria *crit = [category criteriaAt:index];
	int value = [crit value];
	EasyTipAppDelegate *app = [EasyTipAppDelegate getInstance];
	[app accumulate:category criterion:crit];
	NSLog(@"Have %d for criterion %@ and total %d", value, crit.name, [app totalValue]);
	
	// Push a new category controller on the navigation or finish up
	Category *next = category.next;
	if (next) {
		CategoryTableViewController *controller = [[CategoryTableViewController alloc] init:next]; 
		[self.navigationController pushViewController:controller animated:YES];
		[controller release];	
	} else {
		SummaryViewController *controller = [[SummaryViewController alloc] initWithNibName:@"SummaryView" bundle:nil];
		[self.navigationController pushViewController:controller animated:YES];
		[controller release];			
	}
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


- (void)dealloc {
    [super dealloc];
}


@end

